package net.ibizsys.eclipse.studio.xml;

import java.util.Collections;

import org.w3c.dom.Node;

import net.ibizsys.eclipse.studio.util.StringKit;

/**
 * 
 * @author <a href='http://www.weibo.com/xjtoo' target='_blank'>Enmaai</a>
 */
public class IBizApplication extends IBizSearchableNode
{
	public static final String NODE_PSAPPLICATION = "PSAPPLICATION";

	/**
	 * 类型
	 */
	public static final String ATTR_TYPE = "TYPE";

	/**
	 * 代码名称
	 */
	public static final String ATTR_CODENAME = "CODENAME";

	private String codeName;
	private String type;

	private IBizAppModuleList appmoduleList = new IBizAppModuleList();
	private IBizViewList viewList = new IBizViewList();

	@Override
	protected void initAttribute(String name, String value)
	{
		if (StringKit.IsEqual(ATTR_CODENAME, name, true))
		{
			this.codeName = value;
			return;
		}
		if (StringKit.IsEqual(ATTR_TYPE, name, true))
		{
			this.type = value;
			return;
		}
		super.initAttribute(name, value);
	}

	@Override
	protected void initChildNode(Node node, IBizProject project)
	{
		if (StringKit.IsEqual(node.getNodeName(), IBizAppModuleList.NODE_PSAPPMODULES, true))
		{
			this.appmoduleList.init(node, project);
			return;
		}
		if (StringKit.IsEqual(node.getNodeName(), IBizViewList.NODE_PSAPPVIEWS, true))
		{
			this.viewList.init(node, project);
			return;
		}

		super.initChildNode(node, project);
	}

	public String getCodeName()
	{
		return codeName;
	}

	public String getType()
	{
		return type;
	}

	public IBizAppModuleList getAppmoduleList()
	{
		return appmoduleList;
	}

	public IBizViewList getViewList()
	{
		return viewList;
	}

	public IBizView findViewById(String id)
	{
		for (IBizView view : viewList)
		{
			if (StringKit.IsEqual(view.getId(), id, true))
			{
				return view;
			}
		}

		return null;
	}

	@Override
	public void sortChildren()
	{
		Collections.sort(appmoduleList, new XmlNodeTextComparator<XmlNode>());
		Collections.sort(viewList, new XmlNodeTextComparator<XmlNode>());
	}
}
